﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

using WebApp.ConfigEntity;
using WebApp.Services;

namespace WebApp
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
           
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //ICacheService 集中注册服务
            foreach (var item in GetClassName("ICacheService"))
            {
                services.AddSingleton(item.Key, item.Key);
                //foreach (var typeArray in item.Value)
                //{
                //services.AddSingleton(typeArray, item.Key);
                //}
            }

            foreach (var item in GetClassName("IDataBase"))
            {
                services.AddScoped(item.Key, item.Key);
            }


            

            services.AddMvc(options =>
            {
                options.CacheProfiles.Add("Default",
                    new CacheProfile()
                    {
                        Duration = 60
                    });
                options.CacheProfiles.Add("Never",
                    new CacheProfile()
                    {
                        Location = ResponseCacheLocation.None,
                        NoStore = true
                    });
            });
            //services.AddResponseCompression();
            services.AddResponseCaching();
            services.AddMemoryCache();
            services.AddOptions();
            services.Configure<DbConnectionConfiguration>(Configuration.GetSection("DbConnectionConfiguration"));
            services.AddSingleton<DbConnectionConfigurationServicecs>();

            //services.AddDbContext<SqlLiteContextService>();


            // services.AddDbContextPool<DataContext>(options => options.UseSqlite(connection));

            //services.AddSingleton<MemoryCacheService>();
            //services.AddSingleton(typeof(ICacheService),typeof(RedisCacheService));

            //var r = services.Any(c => c.ServiceType == typeof(RedisCacheService));
            //var m = services.Any(c => c.ServiceType == typeof(MemoryCacheService));

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseMvc();
            app.UseResponseCaching();
        }



        private Dictionary<Type, Type[]> GetClassName(string assemblyName)
        {
            if (!String.IsNullOrEmpty(assemblyName))
            {
                // typeof(Assembly(assemblyName))
                // Assembly.GetAssembly(typeof(class));
                var assembly = Assembly.GetExecutingAssembly();
                //  Assembly assembly = Assembly.Load(assemblyName);
                List<Type> ts = assembly.GetTypes().ToList();

                var result = new Dictionary<Type, Type[]>();
                foreach (var item in ts.Where(s => !s.IsInterface))
                {
                    var interfaceType = item.GetInterfaces();
                    var name = interfaceType.FirstOrDefault(c=>c.Name.Contains(assemblyName))?.Name ?? "";
                    if (!string.IsNullOrWhiteSpace(name))
                    {
                        result.Add(item, interfaceType);
                    }

                }
                return result;
            }
            return new Dictionary<Type, Type[]>();
        }
    }
}
